package com.vincent.kdp.ui.other;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.vincent.baseapp.util.view.HorizontalDividerItemDecoration;
import com.vincent.kdp.Activity.CostDetailActivity;
import com.vincent.kdp.R;
import com.vincent.kdp.adapter.MemberSearchAdapter;
import com.vincent.kdp.bean.Paginator;
import com.vincent.kdp.http.RequestCallBack;
import com.vincent.kdp.adapter.UncheckGoodsListAdapter;
import com.vincent.kdp.ui.bean.response.MemberSearchOutput;
import com.vincent.kdp.ui.bean.response.UncheckGoodsListOutput;
import com.vincent.kdp.ui.decoration.DividerItemDecoration;
import com.vincent.kdp.ui.other.repository.UncheckGoodsListRepository;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by pg on 2017/1/24.
 */

public class UncheckGoodsListActivity  extends AppCompatActivity {


    @Bind(R.id.back_img)
    ImageView mBackImg;
    @Bind(R.id.toolbar_center_tv)
    TextView mToolbarCenterTv;

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.SwipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

//    private List<UncheckGoodsListOutput.DataBean.ListBean> data = new ArrayList<>();
//    private UncheckGoodsListAdapter adapter= new UncheckGoodsListAdapter(this, data);
    private UncheckGoodsListRepository uncheckGoodsListRepository=new UncheckGoodsListRepository();

    public int pageNum = 1;//请求页数，默认请求第一条
    public  boolean hasNext=true;

    boolean isLoading;

    private UncheckGoodsListActivity thisActivity;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice_list);
        ButterKnife.bind(this);
        initRecycleView();
        reqData();
        mToolbarCenterTv.setText("未盘点列表");
    }

    private void initRecycleView() {
        thisActivity=this;

        swipeRefreshLayout.setProgressViewOffset(false, 70, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, getResources()
                        .getDisplayMetrics()));
        swipeRefreshLayout.setColorSchemeResources(R.color.black);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                reqData();
            }
        });
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,LinearLayoutManager.VERTICAL));
//        recyclerView.setAdapter(adapter);
//        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//                Log.d("test", "StateChanged = " + newState);
//
//
//            }
//
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                Log.d("test", "onScrolled");
//
//                int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
////                if (lastVisibleItemPosition + 1 == adapter.getItemCount()) {
////                    if(thisActivity.hasNext){
////                        Log.d("test", "loading executed");
////
////                        boolean isRefreshing = swipeRefreshLayout.isRefreshing();
////                        if (isRefreshing) {
////                            adapter.notifyItemRemoved(adapter.getItemCount());
////                            return;
////                        }
////                        if (!isLoading) {
////                            isLoading = true;
////                            reqData();
////                        }
////                    }else{
////
////                    }
////
////                }
////            }
//        });

//        adapter.setOnItemClickListener(new UncheckGoodsListAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(View view, int position) {
//                Log.d("test", "item position = " + position);
//
//            }
//
//            @Override
//            public void onItemLongClick(View view, int position) {
//
//            }
//        });

    }

    public void reqData() {
//        uncheckGoodsListRepository.reqUncheckGoodsList(pageNum,new RequestCallBack() {
//            @Override
//            public void onSuccess(Object object) {
//                UncheckGoodsListOutput uncheckGoodsListOutput = (UncheckGoodsListOutput) object;
//                Paginator paginator=uncheckGoodsListOutput.getData().getPaginator();
//                if(uncheckGoodsListOutput.getData().getList().size()==0){
//
//                }else if(thisActivity.hasNext){
//                    data.addAll(uncheckGoodsListOutput.getData().getList());
//                    swipeRefreshLayout.setRefreshing(false);
//                    adapter.notifyItemRemoved(adapter.getItemCount());
//                    thisActivity.pageNum=thisActivity.pageNum+1;
//                    thisActivity.hasNext=paginator.isHasNextPage();
//                    adapter.notifyDataSetChanged();
//                }else{
//                    Log.d("test", "没有更多数据" );
//                    adapter.notifyItemRemoved(adapter.getItemCount());
//                }
//                swipeRefreshLayout.setRefreshing(false);
//                isLoading=false;
//            }
//
//            @Override
//            public void onFalse(String msg) {
//            }
//        });
    }


    @OnClick(R.id.back_img)
    public void onClick() {
        this.finish();
        overridePendingTransition(com.vincent.baseapp.R.anim.fragment_left_in, com.vincent.baseapp.R.anim.fragment_right_out);
    }
}
